#pragma once
#include "../../include/server.hpp"
#include <iostream>
#include <memory>
extern Log lg;
class echoServer
{
private:
    tcpServer _server;
    uint16_t _port;
    size_t _loopNum;
    bool _inactiveFree = false;
    size_t _outTime;
private:
    void protocol(const ptrConnection& pcon, Buffer& buf)
    {
        // 这里需要将读取的数据清空
        std::string msg = "server echo $";
        msg += buf.read(buf.getReadAbleSize());
        pcon->send(msg.c_str(), msg.size());
        pcon->close();
    }

public:
    echoServer(uint16_t port, size_t loopNum) :_server(port, loopNum), _port(port), _loopNum(loopNum) {}
    void setInactiveFree(size_t sec) { _inactiveFree = true; _outTime = sec; }
    void init()
    {
        _server.setProtocolCb([this](const ptrConnection& pcon, Buffer& buf) { this->protocol(pcon, buf);});
        if (_inactiveFree) _server.setInactiveFree(_outTime);
        _server.init();
    }
    void start() { _server.start(); }
};